package com.igeek;

import com.igeek.dao.UserDao;
import com.igeek.entity.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;
import java.util.List;

public class Demo1 {

    public static void main(String[] args)throws Exception {

        //获取配置文件的输入流
        Reader reader = Resources.getResourceAsReader("mybatis.xml");
        //创建sessionFactory的构建器
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //构建sessionFactory
        SqlSessionFactory sqlSessionFactory = builder.build(reader);
        //获得session连接
        SqlSession session = sqlSessionFactory.openSession();

        //获取DAO的实例
        UserDao userDao = session.getMapper(UserDao.class);
        System.out.println(userDao.getClass().getName());
        List<User> list = userDao.findAll();
        for (User user : list) {
            System.out.println(user.getId()+"\t"+user.getName()+"\t"+user.getPassword()+"\t"+user.getSex()+"\t"+user.getBirthday());
        }

        //关闭
        session.close();
    }
}
